package com.qeegoo.b2bautozityp.moudle.good.viewmodel.category;

import android.content.Intent;
import android.databinding.ObservableField;
import android.support.v4.app.Fragment;

import com.kelin.mvvmlight.command.ReplyCommand;
import com.qeegoo.b2bautozityp.R;
import com.qeegoo.b2bautozityp.api.HttpParams;
import com.qeegoo.b2bautozityp.api.ProgressSubscriber;
import com.qeegoo.b2bautozityp.api.RequestApi;
import com.qeegoo.b2bautozityp.api.RetrofitService;
import com.qeegoo.b2bautozityp.moudle.good.adapter.CategoryAdapter;
import com.qeegoo.b2bautozityp.moudle.good.entity.MySection;
import com.qeegoo.b2bautozityp.moudle.good.model.GoodsCategoryBean;
import com.qeegoo.b2bautozityp.moudle.good.view.category.GoodsCategoryActivity;
import com.qeegoo.b2bautozityp.moudle.good.view.category.GoodsCategoryFragment;

import java.util.ArrayList;

import base.lib.util.ActivityManager;

/**
 * 王少岩 在 2017/9/19 创建了它
 */

public class GoodsCategoryFragViewModel {
    public ObservableField<String> title = new ObservableField<>("");
    private RequestApi mRequestApi;
    private CategoryAdapter mAdapter;
    private ArrayList<MySection<GoodsCategoryBean>> mMySections = new ArrayList<>();
    private Fragment mFragment;

    public GoodsCategoryFragViewModel(Fragment fragment, RequestApi requestApi) {
        mFragment = fragment;
        mRequestApi = requestApi;
        mAdapter = new CategoryAdapter(mMySections);
        mAdapter.setOnItemClickListener((adapter, view, position) -> {
            MySection<GoodsCategoryBean> section = mMySections.get(position);
            if (!section.isHeader) {
                if (section.t.level.equals("4")) {
                    Intent data = new Intent();
                    data.putExtra("label", "绑定分类");
                    data.putExtra("code", section.t.id);
                    data.putExtra("name", section.t.name);
                    ActivityManager.getActivity().setResult(ActivityManager.getActivity().RESULT_OK, data);
                    ActivityManager.getActivity().finish();
                } else {
                    mFragment.getFragmentManager().beginTransaction()
                            .add(R.id.layout_container, GoodsCategoryFragment.newsInstance(section.t.name, section.t.id))
                            .addToBackStack(null)
                            .commitAllowingStateLoss();
                }
            }
        });
    }

    public CategoryAdapter getAdapter() {
        return mAdapter;
    }

    public void getCategorys(String id) {
        mRequestApi.listProductByLevel(HttpParams.goodsCategory(id))
                .compose(RetrofitService.applySchedulers())
                .map(new RetrofitService.HttpResultFunc<>())
                .subscribe(new ProgressSubscriber<ArrayList<GoodsCategoryBean>>() {
                    @Override
                    public void onNext(ArrayList<GoodsCategoryBean> goodsCategorys) {
                        for (int i = 0; i < goodsCategorys.size(); i++) {
                            GoodsCategoryBean bean = goodsCategorys.get(i);
                            if (i == 0)
                                mMySections.add(new MySection(true, "分类"));
                            mMySections.add(new MySection<>(bean));
                        }
                        mAdapter.notifyDataSetChanged();
                    }
                });
    }

    public ReplyCommand leftCommand = new ReplyCommand(() -> {
        if (mFragment.getFragmentManager().getBackStackEntryCount() == 0)
            ((GoodsCategoryActivity) ActivityManager.getActivity()).closeFrag();
        else
            mFragment.getFragmentManager().popBackStack();
    });
}
